package com.globopt.global.old;
import java.util.Random;

/**
 * This is helper class for the matlab operations.
 * 
 * @author Richard Gasztany
 * 
 */
class MatlabOperationHelpers {

	/**
	 * 
	 * @param a
	 * @return
	 */
	public static String arrayToString(double[] a) {
		String ret = "";
		for (int i = 0; i < a.length; i++) {
			ret += a[i] + " ";
		}
		return ret;
	}

	/**
	 * 
	 * @param a
	 * @return
	 */
	public static String arrayToString(double[][] a) {
		String ret = "";
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a[0].length; j++) {
				ret += a[i][j] + " ";
			}
			ret += "\n";
		}
		return ret;
	}

	/**
	 * 
	 * @param x
	 * @param y
	 * @return
	 */
	public static double[] vectorMultiply(double[] x, double[] y) {
		double[] ret = new double[x.length];
		for (int i = 0; i < ret.length; i++) {
			ret[i] = x[i] * y[i];
		}
		return ret;
	}

	/**
	 * 
	 * @param x
	 * @param y
	 * @return
	 */
	public static double[] vectorMultiply(double x, double[] y) {
		double[] ret = new double[y.length];
		for (int i = 0; i < ret.length; i++) {
			ret[i] = x * y[i];
		}
		return ret;
	}

	/**
	 * 
	 * @param x
	 * @param y
	 * @return
	 */
	public static double[] vectorAddition(double[] x, double[] y) {
		double[] ret = new double[x.length];
		for (int i = 0; i < ret.length; i++) {
			ret[i] = x[i] + y[i];
		}
		return ret;
	}

	/**
	 * 
	 * @param x
	 * @param y
	 * @return
	 */
	public static double[] vectorAddition(double[] x, double y) {
		double[] ret = new double[x.length];
		for (int i = 0; i < ret.length; i++) {
			ret[i] = x[i] + y;
		}
		return ret;
	}

	/**
	 * 
	 * @param f
	 * @return
	 */
	public static int maxIndex(double[] f) {
		int ret = 0;
		for (int i = 1; i < f.length; i++) {
			if (f[ret] < f[i]) {
				ret = i;
			}
		}
		return ret;
	}

	public static int[] sortIndex(double[] f) {
		int[] ret = new int[f.length];
		for (int i = 0; i < ret.length; i++) {
			ret[i] = i;
		}
		for (int i = 0; i < f.length; i++) {
			int minindex = i;
			for (int j = i + 1; j < f.length; j++) {
				if (f[j] < f[minindex]) {
					minindex = j;
				}
			}
			int c;
			c = ret[i];
			ret[i] = ret[minindex];
			ret[minindex] = c;
		}
		return ret;
	}

	public static double[] sortByIndex(double[] f, int[] is) {
		double[] ret = f.clone();
		for (int i = 0; i < f.length; i++) {
			ret[i] = f[is[i]];
		}
		return ret;
	}

	public static int[] sortByIndex(int[] f, int[] is) {
		int[] ret = f.clone();
		for (int i = 0; i < f.length; i++) {
			ret[i] = f[is[i]];
		}
		return ret;
	}

	public static double[][] sortByIndex(double[][] x, int[] is) {
		double[][] ret = x.clone();
		for (int i = 0; i < x[0].length; i++) {
			for (int j = 0; j < x.length; j++) {
				ret[j][i] = x[j][is[i]];
			}
		}
		return ret;
	}

	public static double[][] copyColumn(double[][] x, int xi, double[][] y, int yi) {
		double[][] ret = x.clone();
		for (int i = 0; i < x.length; i++) {
			ret[i][xi] = y[i][yi];
		}
		return ret;
	}

	public static int[] subArray(int[] a, int i, int j) {
		int[] ret = new int[j - i + 1];
		for (int k = 0; k < ret.length; k++) {
			ret[k] = a[i + k];
		}
		return ret;
	}

	public static int[] searchIndex(int[] a, int value) {
		int number = 0;
		for (int i = 0; i < a.length; i++) {
			if (a[i] == value) {
				number++;
			}
		}
		int[] ret = new int[number];
		number = 0;
		for (int i = 0; i < a.length; i++) {
			if (a[i] == value) {
				ret[number] = i;
				number++;
			}
		}
		return ret;
	}

	public static int[] searchIndexLess(double[] a, double value) {
		int number = 0;
		for (int i = 0; i < a.length; i++) {
			if (a[i] < value) {
				number++;
			}
		}
		int[] ret = new int[number];
		number = 0;
		for (int i = 0; i < a.length; i++) {
			if (a[i] < value) {
				ret[number] = i;
				number++;
			}
		}
		return ret;
	}

	public static int[] searchIndexMore(double[] a, double value) {
		int number = 0;
		for (int i = 0; i < a.length; i++) {
			if (a[i] > value) {
				number++;
			}
		}
		int[] ret = new int[number];
		number = 0;
		for (int i = 0; i < a.length; i++) {
			if (a[i] > value) {
				ret[number] = i;
				number++;
			}
		}
		return ret;
	}

	public static double[][] twoDimensionalArraysFillWithRandomNumber(int nn100, int nvars) {
		Random random = new Random();
		double[][] ret = new double[nn100][nvars];
		for (int i = 0; i < nn100; i++) {
			for (int j = 0; j < nvars; j++) {
				ret[i][j] = random.nextDouble();
			}
		}

		return ret;
	}

	public static double norm2(double[] a) {
		double ret = 0.0;
		for (int i = 0; i < a.length; i++) {
			ret += a[i] * a[i];
		}
		return Math.sqrt(ret);
	}

	public static double globfun(double[] x, Function fun, double[] pmin, double[] pmax) {
		double[] xr = vectorAddition(vectorMultiply(pmax, x), pmin);
		return fun.evaluation(xr);
	}

}